var ImageGallery = {
	show: function(imgs, width, height){
		var boss = this;
		sbl.dialog({
			width: width,
			height: height,
			contentDivId: 'ImagesBigDialog',
			OnComplete: function(){
				var htmlContent = "<div id='pre' current='0' style='float: left;text-align: center;' title='上一张'>";
				htmlContent += "<img src='" + burl + "/images/pre.png' style='margin-top: 200px;' /></div>";
				htmlContent += "<div id='showImgDiv' align='center' style='width: 80%;height: 90%;float: left;'>";
				htmlContent += "<img id='ShowImages' src='" + imgs[0] + "' /></div>";
				htmlContent += "<div id='next' current='0' style='float: right;text-align: center;' title='下一张'>";
				htmlContent += "<img src='" + burl + "/images/next.png' style='margin-top: 200px;' /></div>";
				$("#ImagesBigDialog").html(htmlContent);
				$("#pre, #next").css("width", width * 0.1);
				$("#showImgDiv").css("width", width * 0.8 - 2);
				$("#pre, #next, #showImgDiv").css("height", height * 0.9);
				$("#next").css("margin-top", "-6px");
				$("#pre, #next").bind("mouseover", function(){
					$(this).css("background", "#ccc");
				});
				$("#pre, #next").bind("mouseleave", function(){
					$(this).css("background", "#FFF");
				});
				if(imgs.length == 0){
					$("#ShowImages").css("width", width * 0.8);
					$("#ShowImages").css("height", height * 0.9);
				}else{
					boss.resize($("#ShowImages"), width * 0.8, height * 0.9);
					boss.BtnEvent(imgs, width, height);
				}
			}
		});
	},
	BtnEvent: function(imgs, width, height){
		var boss = this;
		$("#pre").bind("click", function(){
			var index = $(this).attr("current");
			if(index <= 0){
				index = imgs.length - 1;
			}else{
				index = index - 1; 
			}
			$(this).attr("current", index);
			$("#next").attr("current", index);
			$("#ShowImages").attr("src", imgs[index]);
			boss.resize($("#ShowImages"), width, height * 0.9);
		});
		$("#next").bind("click", function(){
			var index = $(this).attr("current");
			if(index >= imgs.length - 1){
				index = 0;
			}else{
				index = parseInt(index) + 1;
			}
			$(this).attr("current", index);
			$("#pre").attr("current", index);
			$("#ShowImages").attr("src", imgs[index]);
			boss.resize($("#ShowImages"), width, height * 0.9);
		});
	},
	resize: function(option, width, height){
		var img = new Image();
		option.hide();
		img.onload = function(){
			var _width = width / img.width;
			var _height = height / img.height;
			if(_width >= _height){
				img.width = img.width * _height;
				img.height = img.height * _height;
			}else{
				img.width = img.width * _width;
				img.height = img.height * _width;
			}
			option.css("width", img.width);
			option.css("height", img.height);
			option.show();
		};
		img.src = option.attr("src");
	}
};